/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.dqsy.zhengjuxiaoyan.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.dqsy.zhengjuxiaoyan.entity.FfDataMain;
import com.jeeplus.modules.dqsy.zhengjuxiaoyan.mapper.FfDataMainMapper;
import com.jeeplus.modules.dqsy.zhengjuxiaoyan.entity.FfDataFaoqie;
import com.jeeplus.modules.dqsy.zhengjuxiaoyan.mapper.FfDataFaoqieMapper;
import com.jeeplus.modules.dqsy.zhengjuxiaoyan.entity.FdDataShangghai;
import com.jeeplus.modules.dqsy.zhengjuxiaoyan.mapper.FdDataShangghaiMapper;
import com.jeeplus.modules.dqsy.zhengjuxiaoyan.entity.FfDataWeixian;
import com.jeeplus.modules.dqsy.zhengjuxiaoyan.mapper.FfDataWeixianMapper;
import com.jeeplus.modules.dqsy.zhengjuxiaoyan.entity.FfDataYanshi;
import com.jeeplus.modules.dqsy.zhengjuxiaoyan.mapper.FfDataYanshiMapper;

/**
 * 证据校验Service
 * @author 于金洋
 * @version 2020-10-31
 */
@Service
@Transactional(readOnly = true)
public class FfDataMainService extends CrudService<FfDataMainMapper, FfDataMain> {

	@Autowired
	private FfDataFaoqieMapper ffDataFaoqieMapper;
	@Autowired
	private FdDataShangghaiMapper fdDataShangghaiMapper;
	@Autowired
	private FfDataWeixianMapper ffDataWeixianMapper;
	@Autowired
	private FfDataYanshiMapper ffDataYanshiMapper;

	public FfDataMain get(String id) {
		FfDataMain ffDataMain = super.get(id);
		ffDataMain.setFfDataFaoqieList(ffDataFaoqieMapper.findList(new FfDataFaoqie(ffDataMain)));
		ffDataMain.setFdDataShangghaiList(fdDataShangghaiMapper.findList(new FdDataShangghai(ffDataMain)));
		ffDataMain.setFfDataWeixianList(ffDataWeixianMapper.findList(new FfDataWeixian(ffDataMain)));
		ffDataMain.setFfDataYanshiList(ffDataYanshiMapper.findList(new FfDataYanshi(ffDataMain)));
		return ffDataMain;
	}

	public List<FfDataMain> findList(FfDataMain ffDataMain) {
		return super.findList(ffDataMain);
	}

	public Page<FfDataMain> findPage(Page<FfDataMain> page, FfDataMain ffDataMain) {
		return super.findPage(page, ffDataMain);
	}

	@Transactional(readOnly = false)
	public void save(FfDataMain ffDataMain) {
		super.save(ffDataMain);
		for (FfDataFaoqie ffDataFaoqie : ffDataMain.getFfDataFaoqieList()){
			if (ffDataFaoqie.getId() == null){
				continue;
			}
			if (FfDataFaoqie.DEL_FLAG_NORMAL.equals(ffDataFaoqie.getDelFlag())){
				if (StringUtils.isBlank(ffDataFaoqie.getId())){
					ffDataFaoqie.setTestDataMain(ffDataMain);
					ffDataFaoqie.preInsert();
					ffDataFaoqieMapper.insert(ffDataFaoqie);
				}else{
					ffDataFaoqie.preUpdate();
					ffDataFaoqieMapper.update(ffDataFaoqie);
				}
			}else{
				ffDataFaoqieMapper.delete(ffDataFaoqie);
			}
		}
		for (FdDataShangghai fdDataShangghai : ffDataMain.getFdDataShangghaiList()){
			if (fdDataShangghai.getId() == null){
				continue;
			}
			if (FdDataShangghai.DEL_FLAG_NORMAL.equals(fdDataShangghai.getDelFlag())){
				if (StringUtils.isBlank(fdDataShangghai.getId())){
					fdDataShangghai.setTestDataMain(ffDataMain);
					fdDataShangghai.preInsert();
					fdDataShangghaiMapper.insert(fdDataShangghai);
				}else{
					fdDataShangghai.preUpdate();
					fdDataShangghaiMapper.update(fdDataShangghai);
				}
			}else{
				fdDataShangghaiMapper.delete(fdDataShangghai);
			}
		}
		for (FfDataWeixian ffDataWeixian : ffDataMain.getFfDataWeixianList()){
			if (ffDataWeixian.getId() == null){
				continue;
			}
			if (FfDataWeixian.DEL_FLAG_NORMAL.equals(ffDataWeixian.getDelFlag())){
				if (StringUtils.isBlank(ffDataWeixian.getId())){
					ffDataWeixian.setTestDataMain(ffDataMain);
					ffDataWeixian.preInsert();
					ffDataWeixianMapper.insert(ffDataWeixian);
				}else{
					ffDataWeixian.preUpdate();
					ffDataWeixianMapper.update(ffDataWeixian);
				}
			}else{
				ffDataWeixianMapper.delete(ffDataWeixian);
			}
		}
		for (FfDataYanshi ffDataYanshi : ffDataMain.getFfDataYanshiList()){
			if (ffDataYanshi.getId() == null){
				continue;
			}
			if (FfDataYanshi.DEL_FLAG_NORMAL.equals(ffDataYanshi.getDelFlag())){
				if (StringUtils.isBlank(ffDataYanshi.getId())){
					ffDataYanshi.setTestDataMain(ffDataMain);
					ffDataYanshi.preInsert();
					ffDataYanshiMapper.insert(ffDataYanshi);
				}else{
					ffDataYanshi.preUpdate();
					ffDataYanshiMapper.update(ffDataYanshi);
				}
			}else{
				ffDataYanshiMapper.delete(ffDataYanshi);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(FfDataMain ffDataMain) {
		super.delete(ffDataMain);
		ffDataFaoqieMapper.delete(new FfDataFaoqie(ffDataMain));
		fdDataShangghaiMapper.delete(new FdDataShangghai(ffDataMain));
		ffDataWeixianMapper.delete(new FfDataWeixian(ffDataMain));
		ffDataYanshiMapper.delete(new FfDataYanshi(ffDataMain));
	}

}